// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.parts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.designer.ui.editors.GEFEditorFreeformRootEditPart;
import org.dengues.designer.ui.process.editors.ProcessEditorInput;
import org.dengues.designer.ui.process.models.CompProcess;
import org.dengues.designer.ui.process.policies.ProcessLayoutPolicy;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.editpolicies.SnapFeedbackPolicy;
import org.eclipse.ui.IEditorInput;

/**
 * qzhang class global comment. Detailled comment <br/>
 */
public class CompProcessPart extends AbstractProcessPart {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
     */
    @Override
    protected List getModelChildren() {
        return getCastModel().getElements();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.parts.AbstractProcessPart#getCastModel()
     */
    @Override
    protected CompProcess getCastModel() {
        return (CompProcess) getModel();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.LAYOUT_ROLE, new ProcessLayoutPolicy());
        installEditPolicy("Snap Feedback", new SnapFeedbackPolicy()); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        Figure f = new FreeformLayer();
        f.setBorder(new MarginBorder(3));
        f.setLayoutManager(new FreeformLayout());
        return f;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();
        if (CompProcess.PROP_NODES.equals(prop) || CompProcess.PROP_NOTES.equals(prop) || CompProcess.PROP_BLOCKS.equals(prop)
                || CompProcess.PROP_NODELABELS.equals(prop) || CompProcess.PROP_CONNECTIONLABEL.equals(prop)
                || CompProcess.PROP_NODE_STATUS.equals(prop)) {
            getCastModel().checkProcess();
            refreshChildren();
        }
    }

    private IWarehouseNode node;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getAdapter(java.lang.Class)
     */
    @Override
    public Object getAdapter(Class adapter) {
        if (adapter.equals(IWarehouseNode.class)) {
            if (node == null) {
                RootEditPart rootEditPart = getRoot();
                if (rootEditPart instanceof GEFEditorFreeformRootEditPart) {
                    GEFEditorFreeformRootEditPart rootEditPart2 = (GEFEditorFreeformRootEditPart) rootEditPart;
                    IEditorInput editorInput = rootEditPart2.getEditorInput();
                    if (editorInput instanceof ProcessEditorInput) {
                        node = ((ProcessEditorInput) editorInput).getWarehouseNode();
                    }
                }
            }
            return node;
        }
        return super.getAdapter(adapter);
    }

}
